package tt.dz.biz.service.user;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OfflineIdentityCardHisDAO;
import tt.dz.vo.OfflineIdentityCardHisVO;

@Service("offlineIdentityCardHisService")
public class OfflineIdentityCardHisService
{
  @Resource(name="offlineIdentityCardHisDAO")
  private OfflineIdentityCardHisDAO offlineIdentityCardHisDAO;
  
  public <T> void batchInsertOfflineIdentityCardHis(List<T> originals)
    throws Exception
  {
    this.offlineIdentityCardHisDAO.batchInsertOfflineIdentityCardHis(
      getHistories(originals));
  }
  
  public <T> void insertIdentityAccount(T original)
    throws Exception
  {
    this.offlineIdentityCardHisDAO.insertOfflineIdentityCardHis(getHistory(original));
  }
  
  public List<OfflineIdentityCardHisVO> getOfflineIdentityCardHises(OfflineIdentityCardHisVO offlineIdentityCardHis)
  {
    List<OfflineIdentityCardHisVO> list = this.offlineIdentityCardHisDAO.queryOfflineIdentityCardHisByCondition(offlineIdentityCardHis);
    return list;
  }
  
  protected <T> List<OfflineIdentityCardHisVO> getHistories(List<T> originals)
    throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
  {
    List<OfflineIdentityCardHisVO> histories = new ArrayList();
    OfflineIdentityCardHisVO offlineIdentityCardHis = null;
    

    String managerId = (String)SecurityUtils.getSubject().getSession().getAttribute("dzSessionAttrUserId");
    for (T temp : originals)
    {
      offlineIdentityCardHis = new OfflineIdentityCardHisVO();
      offlineIdentityCardHis.setCardNo(
        (String)temp.getClass().getMethod("getCardNo", new Class[0]).invoke(temp, new Object[0]));
      offlineIdentityCardHis.setState(
        (Integer)temp.getClass().getMethod("getState", new Class[0]).invoke(temp, new Object[0]));
      offlineIdentityCardHis.setCreatedBy(managerId);
      offlineIdentityCardHis.setCreatedTime(new Date());
      histories.add(offlineIdentityCardHis);
    }
    return histories;
  }
  
  protected <T> OfflineIdentityCardHisVO getHistory(T original)
    throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
  {
    OfflineIdentityCardHisVO offlineIdentityCardHis = new OfflineIdentityCardHisVO();
    

    String managerId = (String)SecurityUtils.getSubject().getSession().getAttribute("dzSessionAttrUserId");
    
    offlineIdentityCardHis.setCardNo(
      (String)original.getClass().getMethod("getCardNo", new Class[0]).invoke(original, new Object[0]));
    offlineIdentityCardHis.setState(
      (Integer)original.getClass().getMethod("getState", new Class[0]).invoke(original, new Object[0]));
    offlineIdentityCardHis.setCreatedBy(managerId);
    offlineIdentityCardHis.setCreatedTime(new Date());
    
    return offlineIdentityCardHis;
  }
}
